package com.boarsoft.boar.batch.demo.flow.bigfile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.common.util.StreamUtil;
import com.boarsoft.flow.core.bean.ProcessHandler;

public class WriteDpfm02Part implements ProcessHandler {
	private static final Logger log = LoggerFactory.getLogger(WriteDpfm02Part.class);

	public Object process(String entry, Object data, Throwable throwable) throws Throwable {
		SplitInfo o = (SplitInfo) data;
		RandomAccessFile raf = null;
		OutputStream os = null;
		byte[] b = new byte[o.byteSize];
		try {
			raf = new RandomAccessFile(o.originFile, "r");
			raf.seek(o.startPos);// 移动指针到每“段”开头
			int s = raf.read(b);
			os = new FileOutputStream(o.partFileName);
			os.write(b, 0, s);
			os.flush();
		} catch (IOException e) {
			log.error(e.getMessage());
		} finally {
			StreamUtil.close(os);
			StreamUtil.close(raf);
		}
		return true;
	}
}